// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LanguagesToolbarViewModel.cs" company="WordSteps.com">
//   All rights reserved
// </copyright>
// <summary>
//   Defines the LanguagesToolbarViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace RapidDict.Modules.LanguagesToolbar.Views
{
    using System.Collections.Generic;
    using System.Windows.Input;
    using Microsoft.Practices.Composite.Presentation.Commands;
    using RapidDict.Infrastructure.Interfaces;
    using RapidDict.Infrastructure.Models;

    public class LanguagesToolbarViewModel : ViewModelBase
    {
        private ICommand revertCommand;

        private readonly ILanguagePairService languagePairService;
        private readonly ITranslationService translationService;

        public LanguagesToolbarViewModel(ILanguagePairService languagePairService, ITranslationService translationService)
        {
            this.languagePairService = languagePairService;
            this.translationService = translationService;
            
            this.languagePairService.TargetLanguageChanged += (s, e) => this.NotifyPropertyChanged("TargetLanguage");
            this.languagePairService.SourceLanguageChanged += (s, e) => this.NotifyPropertyChanged("SourceLanguage");
        }

        public string SourceLanguage
        {
            get
            {
                return this.languagePairService.GetSourceLanguage().Name;
            }

            set 
            {
                this.languagePairService.SetSourceLanguage(new Language(value));
            }
        }

        public string TargetLanguage
        {
            get
            {
                return this.languagePairService.GetTargetLanguage().Name;
            }

            set
            {
                this.languagePairService.SetTargetLanguage(new Language(value));
            }
        }

        public string[] LanguagesList
        {
            get
            {
                List<Language> languages = Language.GetLanguagesList();

                var strLanguagesArray = new string[languages.Count];
                int i = 0;
                foreach (Language language in languages)
                {
                    strLanguagesArray[i] = language.Name;
                    i++;
                }

                return strLanguagesArray;
            }
        }

        public ICommand RevertCommand
        {
            get
            {
                if (this.revertCommand == null)
                {
                    this.revertCommand = new DelegateCommand<string>(this.Revert);
                }

                return this.revertCommand;
            }
        }

        private void Revert(string obj)
        {
            string newWord = this.translationService.GetTranslation();
            
            Language source = this.languagePairService.GetSourceLanguage();
            Language target = this.languagePairService.GetTargetLanguage();
            this.languagePairService.SetSourceLanguage(target);
            this.languagePairService.SetTargetLanguage(source);

            this.translationService.SetWord(newWord);
        }
    }
}